package com.ylsk.common.config;

import com.ylsk.common.utils.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfiguration {
  @Value("${spring.redis.redisson.singleServerConfig.address}")
  private String address;
  @Value("${spring.redis.redisson.singleServerConfig.password}")
  private String password;
  @Value("${spring.redis.redisson.singleServerConfig.database}")
  private int database;
  @Bean(destroyMethod = "shutdown")
  RedissonClient redisson() {
    Config config = new Config();
    config.useSingleServer()
            .setAddress(address)
            .setDatabase(database);
            if(StringUtils.isNotEmpty(password)){
              config.useSingleServer().setPassword(password);
            }
    return Redisson.create(config);
  }
}
